
//注册前的数据校验
//1.检验邮箱是否存在即是否已经注册
  //当输入框内容发生改变的时候触发时间
  $(document).ready(function(){
  // 在这里写你的代码...
   valide();
});
  function valide(){
	  //每次进来清空校验信息
	  $("#uMail input").focus();
	  $("#reg_form img-input").removeClass("error");
	  $("#reg_form .emsg span").text("");
	  getCode();
  	//当邮箱完成输入失去焦点时触发
  $("#uMail input").change(function(){
	  //发送ajax 请求验证邮箱是否可用
	  var regMail = this.value;
	  $.ajax({
		  url:"http://localhost:8080/Anime-sky/checkMail",
		  type:"POST",
		  data:"email="+regMail,
		  success: function(result){
			  if(result.code == 520){
				  show_validate_msg("#uMail ","success",result.extend.regx);
				  $("#mreg_btn").attr("ajax-value", "success");
				  $("#getCode").attr("disabled", false);
				   checkCode();
			  }else{
				  show_validate_msg("#uMail ","error",result.extend.regx);
				  $("#mreg_btn").attr("ajax-value", "error");
				  $("#getCode").attr("disabled", true);
				  return false;
			  }
		  }
	  });
  });
  
	  function getCode(){
		//获取邮箱注册码  
		  $("#getCode").on("click",function(){
			  var uMail = $("#uMail input").val();
			  var checkMail =/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;
			  if(!checkMail.test(uMail)){
				  show_validate_msg("#uMail","error","邮箱格式错误!");
				  return false;
			  }else{
				  show_validate_msg("#uMail","success","");
			  }
			  var email = $("#uMail input").val();
			  $.ajax({
				  url:"http://localhost:8080/Anime-sky/getVerifiCode",
				  type:"POST",
				  data:"email="+email,
				  success: function(result){
					  if(result.code == 520){
							 show_validate_msg("#uCode","success",result.extend.regx);
							 $("#mreg_btn").attr("ajax-value", "success");
							 
					  }else{
							//显示标题错误信息
							show_validate_msg("#uMail","error",result.extend.regx);
							$("#mreg_btn").attr("ajax-value", "error");
						  return false;
					  }
				  }
			  });
			  return false;
		  });
	  }
	  
	  
	  function checkCode(){
		//当验证码完成输入失去焦点时触发 判断验证码是否输入正确
		  $("#uCode input").change(function(){
			  //发送ajax 请求验证用户输入的验证码是否正确
			  var regCode = this.value;
			  $.ajax({
				  url:"http://localhost:8080/Anime-sky/checkCode",
				  type:"POST",
				  data:"inCode="+regCode,
				  success: function(result){
					  
					  if(result.code == 520){
						  var emsg = $("#uCode").next("button");
						  show_validate_msg(emsg,"success",result.extend.regx);
						  $("#mreg_btn").attr("ajax-value", "success");
						  checkUname();
					  }else{
						  var emsg = $("#uCode").next("button");
						  show_validate_msg(emsg,"error",result.extend.regx);
						  $("#mreg_btn").attr("ajax-value", "error");
						  return false;
					  }
				  }
			  });
		  });
	  }
	 	
	  
	   function checkUname(){
		 //当昵称完成输入失去焦点时触发
			  $("#uname input").change(function(){
				  //发送ajax 请求昵称是否可用
				  var regUname = this.value;
				  $.ajax({
					  url:"http://localhost:8080/Anime-sky/checkUname",
					  type:"POST",
					  data:"userName="+regUname,
					  success: function(result){
						  if(result.code == 520){
							  show_validate_msg("#uname ","success",result.extend.regx);
							  $("#mreg_btn").attr("ajax-value", "success");
						  }else{
							  show_validate_msg("#uname ","error",result.extend.regx);
							  $("#mreg_btn").attr("ajax-value", "error");
							  return false;
						  }
					  }
				  });
				  
			  });
			  
	   }
	  	
    	  //点击注册按钮时
    	  $("#mreg_btn").click(function(){
    	  	 //判断之前的ajax-value是否成功，如果成功才继续往下执行
    		  if($("#mreg_btn").attr("ajax-value") == "error"){
    			  return false;
    		  }
    	      // alert("niamgslkdjfladkfzl");
    	  	  //验证表单信息
    	  	   //先对提交给服务器的数据进行校验
    		  //validate_reg_form();
    	  		 if(!validate_reg_form()){
    	  			 return false;
    	  		 } 
    	  		 
    	  		 //2.发送ajax请求保存新注册的用户
    		  		$.ajax({
    		  			 url:"http://localhost:8080/Anime-sky/uRegister",
    		  			 type:"POST",
    		  			 data:$("#reg_form").serialize(),
    		  			 success:function(result){
    		  				 if (result.code == 520) {
    		  					//alert(result.msg);
    		  					 //注册成功 跳转到
    		  					 $(location).attr("href","http://localhost:8080/Anime-sky/rtimerLink")	 
    		  				}else{
    		  					//显示失败信息 后端验证
    		  					//有那个字段的错误信息就显示那个字段的
    		  					if (undefined != result.extend.errorFields.email) {
    		  						//显示email错误信息
    		  						show_validate_msg("#uMail","error",result.extend.errorFields.email);
    		  					}
    		  					if (undefined != result.extend.errorFields.userName) {
    		  						//显示昵称错误信息
    		  						show_validate_msg("#uname","error",result.extend.errorFields.userName);
    		  					}
    		  					if (undefined != result.extend.errorFields.pwd) {
    		  						//显示密码错误信息
    		  						show_validate_msg("#pwd","error",result.extend.errorFields.pwd);
    		  					}
    		  					
    		  				}
    		  				 
    		  				 
    		  				
    		  			 }
    		  		 });
    		  		return false;	 
    	  		 
    	  });


	  		
	  
  }
//校验表单输入的数据
  function validate_reg_form(){
	  //每次调用函数前再进行清空一次
	  $("#reg_form img-input").removeClass("error");
	  $("#reg_form .emsg span").text("");
	  //1.拿到要校验的数据，使用正则表达式
	  var uMail = $("#uMail input").val();
	 // alert("yanzhengyouxiang");
	  var checkMail =/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;
	  if(!checkMail.test(uMail)){
		  show_validate_msg("#uMail","error","邮箱格式错误!");
		  return false;
	  }else{
		  show_validate_msg("#uMail","success","");
	  }
	  //校验验证码
	  var uCode =$("#uCode input").val()
	  var checkUcode = /^\S+$/;
	  //alert("uuuuuu"+uCode);
	  if (!checkUcode.test(uCode)) {
		  //alert("第一遍校验");
		  var uCode = $("#uCode").next("button");
		show_validate_msg(uCode,"error","请输入验证码！");
		return false;
	}
	  //校验昵称
	 var uname = $("#uname input").val();
	 var checkUname =/^(?!_)(?!.*?_$)([a-zA-Z0-9_\u4e00-\u9fa5]{4,10}|[a-zA-Z0-9_]{1,2}[\u4e00-\u9fa5]{1,2}|[\u4e00-\u9fa5]{1,2}[a-zA-Z0-9_]{1,2}|[\u4e00-\u9fa5]{2,10}|[a-zA-Z0-9_]{4,20})$/;
	
	 if(!checkUname.test(uname)){
		 show_validate_msg("#uname","error","昵称不能以下划线开头或结尾，2-10个汉字或汉字和字母，或者4-20个英文字母");
		 return false;
	 }else{
		 show_validate_msg("#uname","success","");
	 }
	 var pwd = $("#pwd input").val();
	 var checkPwd = /^\S{6,20}$/;
	 // alert(pwd);
	 if (!checkPwd.test(pwd)) {
		 show_validate_msg("#pwd","error","密码长度为6-20位！");
		 return false;
	}else{
		show_validate_msg("#pwd","success","");
	}
	 
	 
	 var npwd =$("#pwd input").val();
	 var rpwd = $("#rep_pwd input").val();
	 if (npwd!=rpwd) {
		 show_validate_msg("#rep_pwd","error","两次密码不一致请重新输入");
		 return false;
	}else{
		show_validate_msg("#rep_pwd","success","");
	}
	 return true;
	  
  }
			  
 //显示校验结果的提示信息
  function show_validate_msg(ele,status,msg){
	  //清除当前元素校验状态
	  
	  if(status == "success"){
		 // alert("验证成功");
	  	$(ele).removeClass("error");
	  	$(ele).next(".emsg").find("span").text(msg);
	  }else if(status == "error"){
		//  alert("验证失败");
	  	$(ele).addClass("error");
	  	$(ele).next(".emsg").find("span").text(msg);
	  }
	  
  }


	
	  

